import 'package:flutter/material.dart';

extension WidgetExt on Widget {
  Widget backgroundColor(Color color) => Container(color: color, child: this);

  Widget expanded({int flex = 1}) => Expanded(flex: flex, child: this);

  Widget weight(int weight) => Expanded(flex: weight, child: this);

  Widget clickable(VoidCallback onTap) => GestureDetector(onTap: onTap, child: this);

  ///带水波纹效果的点击事件
  Widget onClick(VoidCallback onTap, {double radius = 0.0}) =>
      Material(
        color: Colors.transparent,
        child: InkWell(
          onTap: onTap,
          borderRadius: BorderRadius.circular(radius), //圆角水波纹
          child: this,
        ),
      );

  /// 为任意组件设置宽高比
  Widget withAspectRatio({
    required double ratio, // 宽 / 高
    double? width,
    double? height,
  }) {
    assert(ratio > 0, 'Aspect ratio must be greater than 0');

    final isFiniteWidth = width != null && width.isFinite;
    final isFiniteHeight = height != null && height.isFinite;

    // ✅ 情况1：有合法 width，优先使用
    if (isFiniteWidth) {
      return SizedBox(width: width, height: width / ratio, child: this);
    }

    // ✅ 情况2：合法 height，使用 height 推导 width
    if (isFiniteHeight) {
      return SizedBox(height: height, width: height * ratio, child: this);
    }

    // ✅ 情况3：无法直接得出尺寸 —— 使用 LayoutBuilder 推导
    return LayoutBuilder(
      builder: (context, constraints) {
        double usableWidth = constraints.maxWidth;
        double usableHeight = constraints.maxHeight;

        bool canUseWidth = usableWidth.isFinite;
        bool canUseHeight = usableHeight.isFinite;

        if (canUseWidth) {
          return SizedBox(
            width: usableWidth,
            height: usableWidth / ratio,
            child: this,
          );
        } else if (canUseHeight) {
          return SizedBox(
            height: usableHeight,
            width: usableHeight * ratio,
            child: this,
          );
        } else {
          // ✅ 兜底：提供一个默认尺寸（比如 100）
          const fallbackSize = 100.0;
          return SizedBox(
            width: fallbackSize,
            height: fallbackSize / ratio,
            child: this,
          );
        }
      },
    );
  }

  /// 拦截返回键
  Widget interceptBackPress({VoidCallback? callback}) =>
      PopScope(
        canPop: false,
        onPopInvoked: (didPop) {
          callback?.call();
        },
        child: this,
      );

  Widget align({
    Alignment alignment = Alignment.center,
    double? widthFactor,
    double? heightFactor,
  }) => Align(alignment: alignment, widthFactor: widthFactor, heightFactor: heightFactor, child: this);
}

extension FlexExt on Flex {
  //todo 不建议这样写扩展，会把原布局的一些其他样式丢掉,不太行,如果要用的话，得把所有的属性one by one赋值一遍
  @Deprecated("不建议这样写扩展，会把原布局的一些其他样式丢掉")
  Flex space(double space) =>
      Flex(
        direction: direction,
        children: [
          for (int i = 0; i < children.length; i++) ...[
            children[i],
            if (i != children.length - 1) direction == Axis.vertical ? SizedBox(height: space) : SizedBox(width: space), // 最后一个不加
          ]
        ],
      );
}

extension TextExt on Text {}
